package com.libgdxbar.pvzlikegame.screens.gamescreens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.*;
import com.badlogic.gdx.utils.ScreenUtils;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.libgdxbar.pvzlikegame.screens.GameScreenAdapter;

public class UISkinThemeScreen extends GameScreenAdapter {
	private Stage stage;
	private Skin skin;
	private float layoutInScreenScl = 4 / 5f;
	private float layout_padding = 80;
	private float cell_margin = 30;
	private float cell_width = -1;
	private float cell_height = 80;

	private Class<?>[] screenMapping = {
		MainGameScreen.class,
		SplashScreen.class,
		ExceptionCaptorTestScreen.class,
		ShapeAndTextureEntityScreen.class
	};

	@Override
	public void create() {
		skin = new Skin(Gdx.files.internal("ui_skins/shade/skin/uiskin.json"));
		skin.getFont("font-button").getData().setScale(2.5f);

		// 创建舞台
		stage = new Stage(getViewport());
		getImp().addProcessor(stage);

		float stageWidth = stage.getWidth();
		float stageHeight = stage.getHeight();

		//创建主视图
		Table rootTable = new Table();
		float fix = layoutInScreenScl;
		rootTable.setSize(stageWidth * fix, stageHeight * fix);  // 设置 rootTable 为舞台大小的一半

		// 将 rootTable 居中
		Vector2 rootTablePos = new Vector2();
		rootTablePos.set(stageWidth / 2, stageHeight / 2);
		rootTablePos.sub(rootTable.getWidth() / 2, rootTable.getHeight() / 2);
		rootTable.setPosition(rootTablePos.x, rootTablePos.y);
		stage.addActor(rootTable);

		// 按钮列表
		Table buttonList = getButtonTable();

		// 将按钮列表包裹在滚动面板中
		ScrollPane scrollPane = new ScrollPane(buttonList, skin);
		scrollPane.setScrollingDisabled(true, false); // 禁用水平滚动，启用垂直滚动
		scrollPane.setFadeScrollBars(true); // 保持滚动条可见
		rootTable.add(scrollPane).expand().fill();
	}

	//返回vertical按钮列表Table
	private Table getButtonTable() {

		Table buttonTable = new Table();
		buttonTable.pad(layout_padding)//内边距
			.defaults().space(cell_margin); //默认单元格间距

		//创建按钮列表
		for (Class<?> key : screenMapping) {
			String title = key.getSimpleName();
			TextButton button = new TextButton(title, skin);
			Cell<TextButton> cell = buttonTable.add(button);

			if (cell_width > -1) cell.width(cell_width);
			else cell.expandX().fillX();
			cell.height(cell_height);

			buttonTable.row();
			button.addListener(new InputListener() {
				private boolean dragged;

				@Override
				public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
					dragged = false;
					return true;
				}

				@Override
				public void touchDragged(InputEvent event, float x, float y, int pointer) {
					dragged = true;
				}

				@Override
				public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
					//if (outBound(x, y)) return;
					if (dragged) return;
					Gdx.app.log("按钮点击", "按钮 " + title + " 被点击！");
					getScreenManager().setCurScreen(key);
				}

				private boolean outBound(float x, float y) {
					float minX = button.getX();
					float minY = button.getY();
					float maxX = minX + button.getWidth();
					float maxY = minY + button.getHeight();
					return x < minX || x > maxX || y < minY || y > maxY;
				}
			});
		}
		return buttonTable;
	}

	@Override
	public void hide() {
		super.hide();
		//Gdx.input.setInputProcessor(null);
	}

	@Override
	public void show() {
		super.show();
		//if(stage != null) Gdx.input.setInputProcessor(stage);
	}

	@Override
	public void render(float delta) {
		ScreenUtils.clear(0.4f, 0.4f, 0.4f, 1);
		stage.act(Gdx.graphics.getDeltaTime());
		stage.draw();
	}

	@Override
	public void resize(int width, int height) {
		stage.getViewport().update(width, height, true);
	}

	@Override
	public void dispose() {
		stage.dispose();
		skin.dispose();
	}
}

